package com.mdp.sms.service;

import com.mdp.core.utils.SequenceFormat;
import com.mdp.sms.api.SmsCache;
import com.mdp.sms.api.SmsCodeServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class DefaultSmsCodeServerService implements SmsCodeServerService{

	
	@Autowired
	SmsCache cache;
	
	public String createSmsCode(String phoneno, int length, String scene){
		String s= SequenceFormat.parse("{rand:"+length+"}");
		while(cache.containsKey(scene+":"+phoneno+":"+s)){
			s= SequenceFormat.parse("{rand:"+length+"}");
		}
		cache.put(scene+":"+phoneno+":"+s, phoneno);
		return s;
	}
	public boolean validateSmsCode(String phoneno,String code,String scene){ 
		if(cache.containsKey(scene+":"+phoneno+":"+code) ){
			return true;
		} 
		return false;
	}
	@Override
	public void setSmsCache(SmsCache cache) {
		// TODO Auto-generated method stub
	     this.cache=cache;
	}

}
